<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/5/6
 * Time: 9:27
 */

namespace app\api\controller\config;
use app\api\controller\HomeCommon;
use app\api\model\NewsCategory;
use app\api\model\Shop;
use app\api\model\ShopStatistics;
use think\Controller;
use think\Request;

class Carousel extends Controller
{
    /**
     * 获取APP的轮播图
     * @param Request $request
     */
    public function getAppCarousel(Request $request){
        $carousel = (new \app\api\model\Carousel())->findOne([['type','eq',1]],"id,img_path");
        $carousel = $carousel->toArray();
        $carousel['img_path'] = serializeMysql($carousel['img_path'],1);
        foreach ($carousel['img_path'] as $ke=>&$val){
            $val = imgUrlToAbsolute($val);
        }
        $category = (new NewsCategory())->selectList([],"id,name,sort","sort desc");
        $category = $category->toArray();
        array_unshift($category,['id'=>'','name'=>'全部','sort'=>100]);
        $config = (new \app\api\model\Config())->getConfigKey();
        $shop_number =$config['virtual_shop_number']+(new Shop())->getCount([['status','eq',2]]);
        $where = [
            ['day','eq',strtotime(date("Y-m-d",time()))],
            ['type','eq',1]
        ];
        $turnover =$config['virtual_turnover']+(new ShopStatistics())->where($where)->sum("number");
        $returnData = [
            'carousel'  =>$carousel,
            'category'  =>$category,
            'shopNumber'=>$shop_number,
            'turnover'=>$turnover,
            'is_show_config'=>$config['is_show_config']
        ];
        return writeJson(200,$returnData);
    }
    /**
     * 获取APP的广告位图
     * @param Request $request
     */
    public function getAppAdvertisement(Request $request){
        $carousel = (new \app\api\model\Carousel())->findOne([['type','eq',2]],"id,img_path");
        $carousel = $carousel->toArray();
        $carousel['img_path'] = serializeMysql($carousel['img_path'],1);
        foreach ($carousel['img_path'] as $ke=>&$val){
            $val = imgUrlToAbsolute($val);
        }
        $advertisement = $carousel['img_path'][0];
        return writeJson(200,['advertisement'=>$advertisement]);
    }
}